package com.mall.controller;

import com.mall.result.Result;
import com.mall.service.ReportService;
import com.mall.vo.ReportOrdersVO;
import com.mall.vo.ReportTop10VO;
import com.mall.vo.ReportTurnoverVO;
import com.mall.vo.ReportUsersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "数据统计相关接口")
public class ReportController {

    private final ReportService reportService;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额统计")
    public Result<ReportTurnoverVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计");
        return Result.success(reportService.turnoverStatistics(begin, end));
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    @ApiOperation("用户统计")
    public Result<ReportUsersVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户统计");
        return Result.success(reportService.userStatistics(begin, end));
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/ordersStatistics")
    @ApiOperation("订单统计")
    public Result<ReportOrdersVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("订单统计");
        return Result.success(reportService.ordersStatistics(begin, end));
    }

    /**
     * 查询销量排名top10
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @ApiOperation("查询销量排名top10")
    public Result<ReportTop10VO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询销量排名top10");
        return Result.success(reportService.top10(begin, end));
    }

    /**
     * 导出最近30天运营数据报表
     * @return
     */
    @GetMapping("/export")
    @ApiOperation("导出最近30天运营数据报表")
    public Result export() {
        log.info("导出最近30天运营数据报表");
        reportService.export();
        return Result.success();
    }
}
